package com.example.demo.config;


import lombok.extern.slf4j.Slf4j;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author zhangfc
 * @date 2022/1/11 11:22
 */
@Component
@Order(1)
@WebFilter(filterName = "myLoggerFilters", urlPatterns = "/*")
@Slf4j
public class MyLoggerFilters implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletResponse response = (HttpServletResponse) servletResponse;
        // 允许进行跨域的主机
        response.setHeader("Access-Control-Allow-Origin", "*");
        // 允许的访问方法
        response.setHeader("Access-Control-Allow-Methods", "POST, GET, PUT, OPTIONS, DELETE, PATCH");
        response.setHeader("Access-Control-Max-Age", "3600");
        response.setHeader("Access-Control-Allow-Headers"
                , "accept,accept-Language,x-requested-with,Content-Type" +
                        ",lyncmed-access-token,user-agent,userid,referer" +
                        ",refreshtoken,username,token");
        filterChain.doFilter(servletRequest, response);
    }

    @Override
    public void destroy() {

    }
}
